19. Exercise: Jackson
Exercise: Jackson
This time, you're going to try serializing UdacisearchClient to JSON (and then deserializing it from JSON), using the Jackson JSON library.
Just like the Java Object Serialization exercise, this is supposed to be just a straightforward write-then-read to make sure you can get the serialization/deserialization to work.
Read the TODO in Main.java and complete it:
- Create a
com.fasterxml.jackson.databind.ObjectMapperinstance and callObjectMapper#registerModule()to register acom.fasterxml.jackson.datatype.jsr310.JavaTimeModule. This Jackson plugin is required to be able to serialize and deserialize the classes from thejava.timepackage. - Next, call
ObjectMapper#writeValue(Files.newBufferedWriter(outputPath), client)to serialize to JSON. - Similarly, call
ObjectMapper#readValue(...)to read the newly created JSON file and deserialize theUdacisearchClientobject. - Add some
System.out.printlns to prove that everything was serialized and deserialized as expected, then run theMainprogram:
javac Main.java
java Main client.json
All done!
TODO List
Task Feedback:
Nice work! You converted a Java class to and from the JSON data format! Working with JSON is pretty easy!
Code
If you need a code on the https://github.com/udacity.
export PATH=/data/jdk-15.0.1/bin:$PATH
export JAVA_HOME=/data/jdk-15.0.1/bin
export CLASSPATH="/home/workspace:/home/workspace/lib/*"